package com.smartdio.mindtrain.gui;

import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.ImageObserver;
import java.awt.Event;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.KeyStroke;
import java.awt.Point;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.JComboBox;

import com.smartdio.mindtrain.Mind;
import com.smartdio.mindtrain.core.ICountingReceiver;
import com.smartdio.mindtrain.core.TrainMan;
import com.smartdio.mindtrain.core.TrainTimer;
import com.smartdio.mindtrain.images.ImageMan;
import com.smartdio.mindtrain.images.ImagePainter;


import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.GridBagLayout;
import java.awt.Panel;

public class MindTrain {

	private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="10,10"
	private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	private JMenu fileMenu = null;
	private JMenu editMenu = null;
	private JMenu helpMenu = null;
	private JMenuItem exitMenuItem = null;
	private JMenuItem aboutMenuItem = null;
	private JMenuItem cutMenuItem = null;
	private JMenuItem copyMenuItem = null;
	private JMenuItem pasteMenuItem = null;
	private JMenuItem saveMenuItem = null;
	private JDialog aboutDialog = null;
	private JPanel aboutContentPane = null;
	private JLabel aboutVersionLabel = null;
	private JToolBar jToolBar = null;
	private JComboBox comboLevels = null;
	private JPanel jPanel = null;
	private JButton btnTest = null;

	private JPanel image = null;

	/**
	 * @return the image
	 */
	public JPanel getImage() {
		if (image == null) {
			image = new JPanel() {
				public void paint(Graphics g) {
					super.paint(g);
					paintImages(g, jFrame);
					
				}
			};
		}
		return image;
	}

	/**
	 * This method initializes jFrame
	 * 
	 * @return javax.swing.JFrame
	 */
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jFrame.setJMenuBar(getJJMenuBar());
			jFrame.setSize(755, 478);
			jFrame.setContentPane(getJContentPane());
			jFrame.setTitle("超右脑图形记忆训练");
		}
		return jFrame;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJToolBar(), BorderLayout.SOUTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jJMenuBar
	 * 
	 * @return javax.swing.JMenuBar
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getFileMenu());
			jJMenuBar.add(getEditMenu());
			jJMenuBar.add(getHelpMenu());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.add(getSaveMenuItem());
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	/**
	 * This method initializes jMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getEditMenu() {
		if (editMenu == null) {
			editMenu = new JMenu();
			editMenu.setText("Edit");
			editMenu.add(getCutMenuItem());
			editMenu.add(getCopyMenuItem());
			editMenu.add(getPasteMenuItem());
		}
		return editMenu;
	}

	/**
	 * This method initializes jMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("Help");
			helpMenu.add(getAboutMenuItem());
		}
		return helpMenu;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Exit");
			exitMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return exitMenuItem;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getAboutMenuItem() {
		if (aboutMenuItem == null) {
			aboutMenuItem = new JMenuItem();
			aboutMenuItem.setText("About");
			aboutMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JDialog aboutDialog = getAboutDialog();
					aboutDialog.pack();
					Point loc = getJFrame().getLocation();
					loc.translate(20, 20);
					aboutDialog.setLocation(loc);
					aboutDialog.setVisible(true);
				}
			});
		}
		return aboutMenuItem;
	}

	/**
	 * This method initializes aboutDialog
	 * 
	 * @return javax.swing.JDialog
	 */
	private JDialog getAboutDialog() {
		if (aboutDialog == null) {
			aboutDialog = new JDialog(getJFrame(), true);
			aboutDialog.setTitle("About");
			aboutDialog.setContentPane(getAboutContentPane());
		}
		return aboutDialog;
	}

	/**
	 * This method initializes aboutContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getAboutContentPane() {
		if (aboutContentPane == null) {
			labelAuthor = new JLabel();
			labelAuthor.setText("作者：麦廷琮（smart）");
			aboutContentPane = new JPanel();
			aboutContentPane.setLayout(new BorderLayout());
			aboutContentPane.add(getAboutVersionLabel(), BorderLayout.CENTER);
			aboutContentPane.add(labelAuthor, BorderLayout.SOUTH);
		}
		return aboutContentPane;
	}

	/**
	 * This method initializes aboutVersionLabel
	 * 
	 * @return javax.swing.JLabel
	 */
	private JLabel getAboutVersionLabel() {
		if (aboutVersionLabel == null) {
			aboutVersionLabel = new JLabel();
			aboutVersionLabel.setText("超右脑图形记忆训练 V0.1");
			aboutVersionLabel.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return aboutVersionLabel;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getCutMenuItem() {
		if (cutMenuItem == null) {
			cutMenuItem = new JMenuItem();
			cutMenuItem.setText("Cut");
			cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
					Event.CTRL_MASK, true));
		}
		return cutMenuItem;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getCopyMenuItem() {
		if (copyMenuItem == null) {
			copyMenuItem = new JMenuItem();
			copyMenuItem.setText("Copy");
			copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
					Event.CTRL_MASK, true));
		}
		return copyMenuItem;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getPasteMenuItem() {
		if (pasteMenuItem == null) {
			pasteMenuItem = new JMenuItem();
			pasteMenuItem.setText("Paste");
			pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
					Event.CTRL_MASK, true));
		}
		return pasteMenuItem;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getSaveMenuItem() {
		if (saveMenuItem == null) {
			saveMenuItem = new JMenuItem();
			saveMenuItem.setText("Save");
			saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
					Event.CTRL_MASK, true));
		}
		return saveMenuItem;
	}

	/**
	 * This method initializes jToolBar
	 * 
	 * @return javax.swing.JToolBar
	 */
	private JToolBar getJToolBar() {
		if (jToolBar == null) {
			jToolBar = new JToolBar();
			jToolBar.add(getComboLevels());
			jToolBar.add(getComboSec());
			jToolBar.add(getBtnPre());
			jToolBar.add(getBtnTest());
			jToolBar.add(getBtnNext());
			jToolBar.add(getJPanel());
		}
		return jToolBar;
	}

	/**
	 * This method initializes comboLevels
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getComboLevels() {
		if (comboLevels == null) {
			comboLevels = new JComboBox();
			comboLevels.setBounds(new Rectangle(16, 1, 100, 30));
			comboLevels.setPreferredSize(new Dimension(120, 27));
		}
		return comboLevels;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
		}
		return jPanel;
	}

	/**
	 * This method initializes btnTest
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnTest() {
		if (btnTest == null) {
			btnTest = new JButton();
			btnTest.setText("test");
			btnTest.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					switchTest();
				}
			});
		}
		return btnTest;
	}

	private  ImageMan imageMan;

	private  TrainMan trainMan;
	
	private ICountingReceiver countReceiver=new ICountingReceiver(){

		/* (non-Javadoc)
		 * @see com.smartdio.mindtrain.core.ICountingReceiver#currentSec(int)
		 */
		@Override
		public void currentSec(int sec) {
			// TODO Auto-generated method stub
			
		}

		/* (non-Javadoc)
		 * @see com.smartdio.mindtrain.core.ICountingReceiver#finished()
		 */
		@Override
		public void hide() {
			
				painter.setTest(true);
			
			image.repaint();
		}

		/* (non-Javadoc)
		 * @see com.smartdio.mindtrain.core.ICountingReceiver#start()
		 */
		@Override
		public void start() {
			// TODO Auto-generated method stub
			
		}

		/* (non-Javadoc)
		 * @see com.smartdio.mindtrain.core.ICountingReceiver#show()
		 */
		@Override
		public void show() {
			// TODO Auto-generated method stub
			
		}
		
		
		
		
	};
	
	private ImagePainter painter;
	private JButton btnPre = null;
	private JButton btnNext = null;
	
	private boolean isStart = true;
	private JLabel labelAuthor = null;
	private JComboBox comboSec = null;

	/**
	 * This method initializes btnPre	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnPre() {
		if (btnPre == null) {
			btnPre = new JButton();
			btnPre.setText("Previous");
			btnPre.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					proviousTrain();
				}
			});
		}
		return btnPre;
	}

	/**
	 * This method initializes btnNext	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnNext() {
		if (btnNext == null) {
			btnNext = new JButton();
			btnNext.setText("Next");
			btnNext.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					 nextTrain();
				}
			});
		}
		return btnNext;
	}

	/**
	 * This method initializes comboSec	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getComboSec() {
		if (comboSec == null) {
			comboSec = new JComboBox();
			comboSec.setPreferredSize(new Dimension(64, 27));
		}
		return comboSec;
	}

	/**
	 * Launches this application
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				
				MindTrain application = new MindTrain();
				application.imageMan = new ImageMan();
				application.trainMan = new TrainMan(application.imageMan);
				//application.trainMan.createTest(0);
				application.painter = new ImagePainter(application.trainMan);
				application.getJContentPane().add(application.getImage(),BorderLayout.CENTER);
				application.initLevels();
				application.getJFrame().setVisible(true);
			}
		});
	}

	protected void initLevels(){
		
		for(int i=1;i<=Mind.levels.length;i++){
			String item ="第"+i+"级"+Mind.levels[i-1]+"图片";
			getComboLevels().addItem(item);
		}
		
		
		getComboSec().addItem("手动");
		for(int i=0;i<TrainTimer.SECLEVEL.length;i++){
			String item=""+TrainTimer.SECLEVEL[i]+"s";
			getComboSec().addItem(item);
		}
		
		getComboSec().setSelectedIndex(getComboSec().getItemCount()-2);
	
	}
	protected void paintImages(Graphics g, ImageObserver observer) {
		painter.draw(g, observer,image.getWidth(),image.getHeight());
	}

	
	protected void proviousTrain(){
		if(trainMan.previous()){
			painter.setTest(false);
			image.repaint();
			
			if(getComboSec().getSelectedIndex()==0)return ;
			
			TrainTimer timer = new TrainTimer(getComboSec().getSelectedIndex()-1, countReceiver);
			(new Thread(timer)).start();
		}
		
	}
	
	protected void nextTrain(){
		trainMan.next(getComboLevels().getSelectedIndex());
		painter.setTest(false);
		image.repaint();
		
		if(getComboSec().getSelectedIndex()==0)return ;
		
		TrainTimer timer = new TrainTimer(getComboSec().getSelectedIndex()-1, countReceiver);
		(new Thread(timer)).start();
	}
	

	protected void switchTest(){
		if(painter.isTest()){
			painter.setTest(false);
		}else{
			painter.setTest(true);
		}
		image.repaint();
	}
}
